package com.zsck.net.Utils;


import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 数据返回统一处理  参考https://www.jianshu.com/p/ff619fea7e22
 *
 * @param <T>
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final String TAG = "BaseObserver";
    private static final String SUCCESS_CODE = "0";
    private static final String NOT_LOGGED_IN_CODE = "402";
    private static final String LOGIN_FAILURE_CODE = "401";
    @Override
    public void onNext(BaseResponse<T> response) {
        if (SUCCESS_CODE.equals(response.getCode())) {
            onSuccess(response.getData());
        } else if (NOT_LOGGED_IN_CODE.equals(response.getCode()) || LOGIN_FAILURE_CODE.equals(response.getCode())) {
            notLoggedIn();
        } else {
            onFailure(new RuntimeException(response.getCode()), response.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {//服务器错误信息处理

        onFailure(e, RxExceptionUtil.exceptionHandler(e));
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {
    }

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e, String errorMsg);

    public abstract void notLoggedIn();

    public abstract void reRequest();

}
